package com.vrv.vap.baseline.service.impl;

import com.vrv.vap.baseline.config.ExcelTemplateConfig;
import com.vrv.vap.baseline.entity.AssetSettings;
import com.vrv.vap.baseline.mapper.AssetSettingsMapper;
import com.vrv.vap.baseline.service.AbstractExcelExportService;
import com.vrv.vap.baseline.util.FileOperationUtil;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Service
public class AssetSettingsExportService extends AbstractExcelExportService<AssetSettings> {
    private static final String key = "asset_settings";
    private final AssetSettingsMapper assetSettingsMapper;

    public AssetSettingsExportService(FileOperationUtil fileOperationUtil, ExcelTemplateConfig excelTemplateConfig, AssetSettingsMapper assetSettingsMapper) {
        super(fileOperationUtil, excelTemplateConfig, key);
        this.assetSettingsMapper = assetSettingsMapper;
    }

    @Override
    protected List<AssetSettings> getAllData() {
        return assetSettingsMapper.selectAll();
    }

    @Override
    protected void processData(List<AssetSettings> dataList, String format) throws IOException {
        for (AssetSettings settings : dataList) {
            String data = settings.getData();
            if (data != null && data.length() > maxCellLength) {
                String fileName = settings.getGuid() + ".txt";
                File file = fileOperationUtil.createFile(fileName, key, format);
                fileOperationUtil.writeContentToFile(file, data);
                settings.setData(fileName);
            }
        }
    }

    @Override
    protected Class<AssetSettings> getEntityClass() {
        return AssetSettings.class;
    }
}